/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/


package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.display.DisplayObject;
	
	import mx.core.Application;
	
	import virtPresenter.event.ChangeViewEvent;
	import virtPresenter.view.VideoPlayer;
	import virtPresenter.view.Default;
	import virtPresenter.model.virtPresenterLocator;	
	
	/**
	 * Command handles when the index of a selected view in the combobox has changed.
	 * */
	public class ChangeViewCommand implements ICommand {	
		
		/**
		 * Model is an instance of the virtPresenterLocator 
		 */
    	private var model:virtPresenterLocator=virtPresenterLocator.getInstance();	
    	
    	private var defaultView:Default=model.defaultView;		
    	
		/**
		 * Handler for ChangeViewEvent.
		 * If the user clicks on a item in the views combobox this method handles
		 * changing the view.
		 * */
		public function execute( event : CairngormEvent ): void
		{
			var ViewChanged : ChangeViewEvent =ChangeViewEvent(event);
			var viewLabel:String=ViewChanged.viewLabel;
			
			switch (viewLabel)
			{
				case "Switch":
					model.views.selectedChild=model.defaultView;
					switchVideoAndSlide();
				break;
				case "Default":
					model.views.selectedChild=model.defaultView;
					setToDefaultView();
				break;
				case "Slide overview":				
					model.views.selectedChild=model.slideBrowserView;
				default:
					setToDefaultView();
				break;	
			}		
		}
		
		//Make Cairngormevent like SwitchVideoSlideEvent from this
		private function switchVideoAndSlide():void{
			//vbChild and pbChild can be presentation or video, depending on the current view
			var vbChild:DisplayObject=defaultView.verticalBox.getChildAt(0);
			var pbChild:DisplayObject=defaultView.presentationBox.getChildAt(0);
			defaultView.verticalBox.removeChildAt(0);
			defaultView.presentationBox.removeChildAt(0);
			defaultView.verticalBox.addChildAt(pbChild,0);
			defaultView.presentationBox.addChildAt(vbChild,0);			
			
			//If we changed from big video to small, resize the video to small.
			if (model.defaultView.presentationBox.getChildByName("videoPlayer")==null){
				model.defaultView.videoPlayer.smallVideo=true;
				model.defaultView.videoPlayer.fitToWindow();				
			}
			else{				
				model.defaultView.videoPlayer.smallVideo=false;
				model.defaultView.videoPlayer.fitBigVideoToWindow();
			}
		}
		
		private function setToDefaultView():void
		{
			//If we already have the defaultView do nothing else set defaultView
			if (model.defaultView.verticalBox.getChildByName("videoPlayer")!=null)
			{
			}
			
			else{
				var presentation:DisplayObject=defaultView.verticalBox.getChildAt(0);
				var video:DisplayObject=defaultView.presentationBox.getChildAt(0);
				defaultView.verticalBox.removeChildAt(0);
				defaultView.presentationBox.removeChildAt(0);
				defaultView.verticalBox.addChildAt(video,0);
				defaultView.presentationBox.addChildAt(presentation,0);
				model.defaultView.videoPlayer.smallVideo=true;
				model.defaultView.videoPlayer.fitToWindow();
			}
		}		
		
		
	}
	
}